import * as GC from "@grapecity-software/spread-sheets";
import "@grapecity-software/spread-sheets-shapes"
import "@grapecity-software/spread-sheets-charts"
import "@grapecity-software/spread-sheets-print"
import "@grapecity-software/spread-sheets-resources-zh"
import "@grapecity-software/spread-sheets-pdf"
import "@grapecity-software/spread-sheets-barcode"
import "@grapecity-software/spread-sheets-tablesheet"
import "@grapecity-software/spread-sheets-reportsheet-addon"
import "@grapecity-software/spread-sheets-slicers"
import "@grapecity-software/spread-sheets-pivot-addon"
import "@grapecity-software/spread-sheets-ganttsheet"
import "@grapecity-software/spread-sheets-datacharts-addon"
import "@grapecity-software/spread-sheets-io"
import "@grapecity-software/spread-excelio"
import "@grapecity-software/spread-sheets-languagepackages"
import "@grapecity-software/spread-sheets-designer-resources-cn"
import "@grapecity-software/spread-sheets-designer"

GC.Spread.Common.CultureManager.culture("zh-cn")

// GC.Spread.Sheets.LicenseKey = "表格控件部署授权"
// GC.Spread.Sheets.Designer.LicenseKey = "在线表格编辑器部署授权"

let config = GC.Spread.Sheets.Designer.DefaultConfig



designer = new GC.Spread.Sheets.Designer.Designer("designer-container", config)
spread = designer.getWorkbook()
sheet = spread.getActiveSheet()


document.getElementById("fetch").addEventListener("click", function () {
    let sheetCount = 20
    console.log("Sheet数量：", sheetCount)
    console.time("从接口拉取数据")
    fetch('http://localhost:3000/large_data_bind').then(response => {
        return response.json()
    }).then(res => {
        console.log(res)
        console.log(`数据量：${Object.keys(res[0]).length}列，${res.length}行`)
        console.timeEnd("从接口拉取数据")

        console.time("setDataSource")
        let colCount = Object.keys(res[0]).length
        let tableStyle = new GC.Spread.Sheets.Tables.TableTheme()
        spread.setSheetCount(sheetCount)
        spread.sheets.forEach((s, i) => {
            s.setColumnCount(colCount)
            let table = s.tables.add("Table" + i, 0, 0, 1, colCount, tableStyle)
            // 设置table的绑定路径
            table.bindingPath("data")
            Object.keys(res[0]).forEach((field, c) => {
                // 设置每一列的绑定路径
                table.setColumnDataField(c, field)
            })
            s.setRowCount(res.length + 1)
            // 设置数据源
            s.setDataSource(new GC.Spread.Sheets.Bindings.CellBindingSource({
                data: res
            }))
        })
        console.timeEnd("setDataSource")
    })
})
document.getElementById("btn").addEventListener("click", function () {
    // 由于数据页是通过数据绑定实现的，而数据绑定的数据源本来默认就不会被保存，所以save时无需reset数据页sheet
    console.time("保存Spread（无需reset）")
    spread.save(function (blob) {
        console.timeEnd("保存Spread（无需reset）")
    })
})